//
//  ViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/5/24.
//

import UIKit

class BHLaunchAdViewController: BHBaseViewController {

    
    lazy var sloganView: UIImageView = {
        let view = UIImageView.init(image: UIImage.init(named: "bh_launch_slogan"))
        view.contentMode = .scaleAspectFit
        return view
    }()
    lazy var logoView: UIImageView = {
        let view = UIImageView.init(image: UIImage.init(named: "bh_launch_logo"))
        view.contentMode = .scaleAspectFit

        return view
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.bh_navigationBar.removeFromSuperview()
        
        self.view.backgroundColor = .white
        self.view.addSubview(self.sloganView)
        self.view.addSubview(self.logoView)
     
        
        self.sloganView.frame = CGRect(x: (UIScreen.width-130.5)/2.0, y: UIScreen.stateBarHeight+130, width: 130.5, height: 105)
        self.logoView.frame = CGRect(x: (UIScreen.width-44)/2.0, y: UIScreen.height-UIScreen.safeBottomHeight-70-44, width: 44, height: 44)
        
        // 用户协议是否同意
        let type = BHCache_UserDefault.objectForKey(key: "bh_application_agreement_save")
        if type != nil { // 已经同意过了
            self.continueLogionOrNot()
        }else {
            let view = KSAgreementShowView.loadFormXib()
            self.view.addSubview(view)
            view.agreementActBlock = { (url, titleText) in
                BHRouter.shared.bh_goWeb(title: titleText, linkUrl: url)
            }
            
            // 同意了
            view.userAgreeActBlock = { [unowned self] in
                self.continueLogionOrNot()
            }
        }
        
        
    }
}

extension BHLaunchAdViewController {
    func continueLogionOrNot()  {
        let token = BHCache.load_userToken()
        if token.isEmpty == true { // 未登录
            self.changeToLogion()
        }else { // 已登录
            BHMeUserInfoModel.shared.token = token
            self.refreshToken()
            
        }
    }
}

// 未登录，切换到登录
extension BHLaunchAdViewController {
    func changeToLogion()  {
        let loginVC = BHLoginViewController.init()
        self.navigationController?.pushViewController(loginVC, animated: true)
    }
}


extension BHLaunchAdViewController {
    func refreshToken()  {
        BHCommonService.refreshTokenService { (success, code, errorMsg) in
            if success == true { //
                self.refreshUser()
            }else {
                self.changeToLogion()
            }
        }
    }
    
    func refreshUser()  {
        let manager = BHMeServiceManager.init()
        manager.responseUserInfoBlock = { model in 
            BHMeUserInfoModel.shared.appendInfo(model: model)
            
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.loginIm()
            
        }
        manager.queryUserInfoService(userId: nil)
    }
}

